package demo.linkdoris;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.*;

public class DruidUtils {
    public static Logger LOG = LoggerFactory.getLogger(DruidUtils.class);
    // doris
//    private static String jdbcUrl =
//            "jdbc:mysql://10.10.16.119:6030"
//                    + "/gdm?serverTimezone=Asia/Shanghai&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8"
//                    + "&zeroDateTimeBehavior=convertToNull&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";
    //super doris
    private static String jdbcUrl = "jdbc:mysql://10.10.13.212:6031/gdm?serverTimezone=Asia/Shanghai&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";
    private static String username = "attribution";
    private static String password = "wetc@#attri";
    // test
//        private static String jdbcUrl = "jdbc:mysql://172.16.1.91:19030" +
//
//     "/gdm?serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8" +
//                "&useOldAliasMetadataBehavior=true&useSSL=false";
//        private static String username = "risk_lb_pt";
//        private static String password = "risk_lb_pt@#123";

    private static String driverName = "com.mysql.jdbc.Driver";
    public static Connection connection = null;
    public static transient ExecutorService executorService = null;
    public static transient DruidDataSource dataSource = null;
    /*连接池最大线程数*/
    private static int maxPoolConn = 10;
    /*静态初始化*/
    static {
        // 创建线程池
        //        executorService = newFixedThreadPool(maxPoolConn);
        ThreadFactory druidThreadFactory =
                new ThreadFactoryBuilder().setNameFormat("Druid—Doris threadPool").build();
        executorService =
                new ThreadPoolExecutor(
                        4,
                        8,
                        5L,
                        TimeUnit.SECONDS,
                        new LinkedBlockingDeque<>(200),
                        druidThreadFactory,
                        new ThreadPoolExecutor.AbortPolicy());
        dataSource = new DruidDataSource();

        dataSource.setDriverClassName(driverName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(jdbcUrl);
        dataSource.setMaxActive(maxPoolConn);
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery("select 1");
        dataSource.setValidationQueryTimeout(1);
    }

    public static Connection getConnection() throws SQLException {
        if (connection == null) {
            LOG.warn("create connection!!");
            connection = dataSource.getConnection();
        } else {
            if (!connection.isValid(1)) {
                LOG.warn("connection timeout killed!!");
                connection.close();
                connection = dataSource.getConnection();
            }
        }
        return connection;
        //        return dataSource.getConnection();
        //        if(connection==null){
        //            connection= dataSource.getConnection();
        //        }
        //        return connection;
    }
}
